@extends('web.frame_layout')
<?php $parent_menu = "购物商城"; $menu_selected = "我的购物车";?>
<link rel="stylesheet" href="{{asset('web/css/cart.css')}}">
<link rel="stylesheet" href="{{asset('web/css/components.css')}}">
{{--<link rel="stylesheet" href="{{asset('web/css/goods-details.css')}}">--}}
{{--<link rel="stylesheet" href="{{asset('web/css/popup.css')}}">--}}
@section('content')
    <div class="m-right-nr">
        <div>
            <h2><span>购物车</span></h2>
        </div>
        <div class="cart-box">
            <h3>全部商品</h3>
            <div class="cart-box-header">
                <h3><input type="checkbox" class="check_all">全选</h3>
                <ul>
                    @foreach($carts as $pro)
                        <li>
                            <div>
                                <input type="hidden" class="product_id" value="{{$pro->id}}">
                                <input type="hidden" class="product_no" value="{{$pro->no}}">

                                <div class="cart-box-left">
                                    <input type="checkbox" class="check_val" value="{{$pro->no}}">
                                    <img src="{{asset($pro->img)}}" alt="">
                                </div>
                                <div class="good-box">
                                    <h3>{{$pro->title}}</h3>
                                    <h4>￥<span class="sp_price">{{$pro->p1}}</span></h4>
                                </div>
                                <div class="jia_jian">
                                    <span class="minus num-change" style="cursor:pointer;">-</span>
                                    <input type="text" class="goods_num" pattern="int"
                                           value="{{$cart[$pro->no]}}">
                                    <span class="add num-change" style="cursor:pointer;">+</span>
                                </div>
                                <div class="del hidden-xs" data="{{$pro->no}}">删除</div>
                            </div>
                        </li>
                    @endforeach
                </ul>
                <div class="cart-box-footer">
                    <input type="checkbox" class="check_all" style="margin: 0 20px 0;">全选 <span
                            class="del-batch" style="cursor:pointer;">删除选中商品</span>
                    <div>总价：<span class="all_pay">0.00</span><a href="###" onclick="payDlg()">立即支付</a></div>
                </div>
            </div>
        </div>
    </div>

@endsection

@section('footer')
    @include('web.coms.pay-dialog')
    <script>
        $(function () {
            // 商品数--
            $('.minus').on('touchend click', function (e) {
                e.preventDefault();
                var goods_num = $(this).next("input[class='goods_num']");
                var num = goods_num.val();
                num > 1 ? num-- : 1;
                goods_num.val(num);
            });

            // 商品数++
            $('.add').on('touchend click', function (e) {
                e.preventDefault();
                var goods_num = $(this).prev("input[class='goods_num']");
                var num = goods_num.val();
                num++;
                goods_num.val(num);
            });

            //单个删除
            $(".del").on("touchend click", function (e) {
                e.preventDefault();
                var del_value = [];
                del_value.push($(this).attr("data"));

                var nos = JSON.stringify(del_value);
                $.get("{{route('web.cart.del')}}" + "/" + nos, function (data) {
                    if (data.code == 0) {
                        toastr["success"]('删除成功');
                        window.location.reload();
                    } else {
                        toastr["error"]('删除失败');
                    }
                });

            });

            //批量删除
            $(".del-batch").on("touchend click", function (e) {
                e.preventDefault();

                var chk_value = [];
                $('.check_val:checked').each(function () {
                    chk_value.push($(this).val());
                });

                if (chk_value.length <= 0) {
                    toastr["error"]('请选中要删除的商品');
                    return false;
                }

                var nos = JSON.stringify(chk_value);

                $.get("{{route('web.cart.del')}}" + "/" + nos, function (data) {
                    if (data.code == 0) {
                        toastr["success"]('删除成功');
                        window.location.reload();
                    } else {
                        toastr["error"]('删除失败');
                    }
                });

            });

            //全选
            $(".check_all").on("touchend click", function (e) {
//                e.preventDefault();
//            console.log($(this).attr("checked"));
                $(".check_val").prop("checked", this.checked);
                $(".check_all").prop("checked", this.checked);
            });


            //选中商品后计算价格
            $('[type="checkbox"]').on('click', function () {
                total_money();
            });

            $(".num-change").on('click', function () {
                total_money();
            });

            $(".goods_num").on("input propertychange", function () {
                total_money()
            });

            function total_money() {
                var total = 0, product_ids = '', product_nos = '', buy_nums = '';
                $(".check_val:checked").each(function () {
                    var id = $(this).closest("li").find(".product_id").val();
                    var no = $(this).closest("li").find(".product_no").val();
                    var num = $(this).closest("li").find(".goods_num").val();
                    var price = $(this).closest("li").find(".sp_price").html();
                    total += parseInt(num) * parseFloat(price);
                    product_ids += id + ',';
                    product_nos += no + ',';
                    buy_nums += num + ',';
                });
                $('.product_ids').empty().val(product_ids);
                $('.product_nos').empty().val(product_nos);
                $(".buy_nums").empty().val(buy_nums);
                $('.all_pay,#payMoney').text(total.toFixed(2));
                $("input[name='payMoney']").val(total.toFixed(2));
            }
        });

    </script>
@endsection